 package CurtainDesign.utils;

 import java.io.PrintStream;
 import java.util.Stack;

 public class CalculateUtils
 {
   public static float SINGLE_HINGE_GAP = 1.59F; // 单开合页间隙

   public static float DOUBLE_HINGE_GAP = 0.0F; // 双开合页间隙

   public static float BEND_HINGE_GAP = 1.59F; // 弯曲合页间隙

   public static float DUIKOU_POLE_GAP = 3.17F; // 对口立杆间隙

   public static float NO_HINGE_GAP = 3.17F; // 无合页间隙

   public static float BYFOLD_FRAME_GAP = 5.5F;

   public static float BYFOLD_SINGLE_HINGE_GAP = 1.59F;

   public static float BYFOLD_DOUBLE_HINGE_GAP = 0.0F;

   public static float BYFOLD_BEND_HINGE_GAP = 1.59F;

   public static float BYFOLD_DUIKOU_POLE_GAP = 1.7F;

   public static float BYFOLD_NO_HINGE_GAP = 1.0F;

   public static char NONE_HINGE = 'N';

   public static char LEFT_SIDE_HINGE = 'L';

   public static char RIGHT_SIDE_HINGE = 'R';

   public static char DOUBLE_SIDE_HINGE = 'D';

   public static HingeRelatedInfo getSimpleRectHingeRelatedInfo(String hingeInstallType)
   {
     hingeInstallType = hingeInstallType.toUpperCase(); // 合页安装方式

     int tPoleNum = 0;

     HingeRelatedInfo info = new HingeRelatedInfo();

     hingeInstallType = hingeInstallType.trim().toUpperCase();
     if (hingeInstallType.indexOf('T') == -1)
     {
       info = info.plus(getSimpleRectDistPaneInfo(hingeInstallType));
     }
     else
     {
       String[] distinctsInstall = hingeInstallType.split("T");
       tPoleNum = distinctsInstall.length - 1;
       int i = 0; for (int j = distinctsInstall.length; i < j; i++)
       {
         info = info.plus(getSimpleRectDistPaneInfo(distinctsInstall[i]));
       }
     }

     info.setTPoleNum(tPoleNum);
     return info;
   }

   public static HingeRelatedInfo getSimpleRectDistPaneInfo(String hingeInstallType)
   {
     int paneNum = 0;

     int singleHingePaneNum = 1;

     int doubleHingePaneNum = 0;

     int bendHingePaneNum = 0;

     int poleNum = 0;

     int duikouPoleNum = 0;

     int shuanglianPoleNum = 0;

     float gap = SINGLE_HINGE_GAP;

     int magnetSliceNum = 0;

     // 合页相关信息
     HingeRelatedInfo info = new HingeRelatedInfo();

     Stack hingeTypeStack = new Stack(); // 合页类型栈

     Stack hingeSidesStack = new Stack(); // 合页边栈

     hingeInstallType = hingeInstallType.trim().toUpperCase();

     Character topSidesChar = null;
     char currChar = '\000';
     char typeChar = '\000';

	   // 遍历合页类型
     int i = 0;
     for (int j = hingeInstallType.length(); i < j; i++)
     {
       currChar = hingeInstallType.charAt(i);
       // 存在1 的窗扇数加一
       if (currChar == '1')
       {
         paneNum++;
       }
       // 为R 或者 L时候的情况
       else if ((currChar == 'R') || (currChar == 'L'))
       {
         if (hingeSidesStack.size() > 0)
         {
           topSidesChar = (Character)hingeSidesStack.peek(); // peek 不删除栈顶的值

           if (currChar != topSidesChar.charValue())
           {
             duikouPoleNum += 2; // 对口立杆数量加1

             singleHingePaneNum++; // 单开合页数量加1

             gap = gap + DUIKOU_POLE_GAP + SINGLE_HINGE_GAP; // 计算合页间隙

			   // 合页类型栈长度为1 && 顶栈为L 瓷片数量加1
             if ((hingeTypeStack.size() == 1) && (topSidesChar.charValue() == 'L'))
             {
               magnetSliceNum++;
             }

             // 当合页类型栈大小大于0时 遍历
             while (hingeTypeStack.size() > 0)
             {
             	// 弹出栈顶元素
               typeChar = ((Character)hingeTypeStack.pop()).charValue();
               // 为S时 单开合页扇数加1 ，间隙加上 单开合页间隙
               if (typeChar == 'S')
               {
                 singleHingePaneNum++;
                 gap += SINGLE_HINGE_GAP;
               }
               // 为B时 弯曲合页数加1，双连立杆数加2，间隙加弯曲合页间隙
               if (typeChar == 'B')
               {
                 bendHingePaneNum++;
                 shuanglianPoleNum += 2;
                 gap += BEND_HINGE_GAP;
               }
               // 为D时 跳过循环
               if (typeChar != 'D')
                 continue;

               // 双开合页扇数加1
               doubleHingePaneNum++;
               // 缝隙加上双开合页间隙
               gap += DOUBLE_HINGE_GAP;
             }

             hingeTypeStack.removeAllElements(); // 删除所有合页类型栈数据

             hingeSidesStack.removeAllElements(); // 删除所有合页边栈数据

             hingeSidesStack.push(new Character(currChar)); // 合页边栈增加数据
           }
           else
           {
             hingeSidesStack.push(new Character(currChar)); // 合页边栈增加数据
           }
         }
         else
         {
           hingeSidesStack.push(new Character(currChar)); // 合页边栈增加数据
         }
       } else {
       	// 不为 B && D & S 时 合页类型栈亚入值
         if ((currChar != 'B') && (currChar != 'D') && (currChar != 'S'))
           continue;
         hingeTypeStack.push(new Character(currChar));
       }

     }

     // 如果 合页类型栈 等于 扇数 - 1
     if (hingeTypeStack.size() == paneNum - 1)
     {
       gap += NO_HINGE_GAP; // 间隙 加上无合页间隙
		 // 合页类型栈 = 1 && 合页边栈顶元素为L， 瓷片数量加1
       if ((hingeTypeStack.size() == 1) && (((Character)hingeSidesStack.peek()).charValue() == 'L'))
       {
         magnetSliceNum++;
       }

     }

     // 遍历合页类型栈
     while (hingeTypeStack.size() > 0)
     {
     	// 弹出栈顶元素
       typeChar = ((Character)hingeTypeStack.pop()).charValue();
       // 如果为S 单开合页扇数加1， 间隙加单开合页间隙
       if (typeChar == 'S')
       {
         singleHingePaneNum++;
         gap += SINGLE_HINGE_GAP;
       }
		 // 如果为B 弯曲合页扇数加1， 双连立杆数量加2， 间隙加弯曲合页间隙
       if (typeChar == 'B')
       {
         bendHingePaneNum++;
         shuanglianPoleNum += 2;
         gap += BEND_HINGE_GAP;
       }
        // 如果为D时，跳过循环
       if (typeChar != 'D')
         continue;
       // 双开合页扇数加1
       doubleHingePaneNum++;
       // 间隙加双开合页间隙
       gap += DOUBLE_HINGE_GAP;
     }

     // 立杆数量 = 窗扇数量 * 2 - 对口立杆数量 - 双连立杆数量
     poleNum = paneNum * 2 - duikouPoleNum - shuanglianPoleNum;

     info.setPaneNum(paneNum); // 扇数量
     info.setSingleHingePaneNum(singleHingePaneNum); // 单开合页扇数量
     info.setDoubleHingePaneNum(doubleHingePaneNum); // 双开合页扇数量
     info.setBendHingePaneNum(bendHingePaneNum); // 弯曲合页扇数量
     info.setPoleNum(poleNum); // 立杆数量
     info.setDuikouPoleNum(duikouPoleNum); // 对口立杆数量
     info.setShuanglianPoleNum(shuanglianPoleNum); // 双连立杆数量
     info.setGap(gap); // 间隙
     info.setMagnetSliceNum(magnetSliceNum); // 瓷片数量
     return info;
   }

   public static HingeRelatedInfo getByFoldHingeRelatedInfo(String hingeInstallType)
   {
     int paneNum = 0;

     int singleHingePaneNum = 0;

     int doubleHingePaneNum = 0;

     int bendHingePaneNum = 0;

     int poleNum = 0;

     int duikouPoleNum = 0;

     int shuanglianPoleNum = 0;

     float gap = BYFOLD_FRAME_GAP;

     int magnetSliceNum = 0; // 瓷片数量

     HingeRelatedInfo info = new HingeRelatedInfo();

     Stack hingeTypeStack = new Stack();

     Stack hingeSidesStack = new Stack();

     hingeInstallType = hingeInstallType.trim().toUpperCase();

     Character topSidesChar = null;
     char currChar = '\000';
     char typeChar = '\000';

     int i = 0; for (int j = hingeInstallType.length(); i < j; i++)
     {
       currChar = hingeInstallType.charAt(i);
       if (currChar == '1')
       {
         paneNum++;
       }
       else if ((currChar == 'R') || (currChar == 'L'))
       {
         if (hingeSidesStack.size() > 0)
         {
           topSidesChar = (Character)hingeSidesStack.peek();

           if (currChar != topSidesChar.charValue())
           {
             duikouPoleNum += 2;

             gap = gap + BYFOLD_DUIKOU_POLE_GAP + BYFOLD_FRAME_GAP;

             if (hingeTypeStack.size() == 1)
             {
               magnetSliceNum++;
             }

             while (hingeTypeStack.size() > 0)
             {
               typeChar = ((Character)hingeTypeStack.pop()).charValue();
               if (typeChar == 'S')
               {
                 singleHingePaneNum++;
                 gap += BYFOLD_SINGLE_HINGE_GAP;
               }
               if (typeChar == 'B')
               {
                 bendHingePaneNum++;
                 shuanglianPoleNum += 2;
                 gap += BYFOLD_BEND_HINGE_GAP;
               }
               if (typeChar != 'D')
                 continue;
               doubleHingePaneNum++;
               gap += BYFOLD_DOUBLE_HINGE_GAP;
             }

             hingeTypeStack.removeAllElements();

             hingeSidesStack.removeAllElements();

             hingeSidesStack.push(new Character(currChar));
           }
           else
           {
             hingeSidesStack.push(new Character(currChar));
           }
         }
         else
         {
           hingeSidesStack.push(new Character(currChar));
         }
       } else {
         if ((currChar != 'B') && (currChar != 'D') && (currChar != 'S'))
           continue;
         hingeTypeStack.push(new Character(currChar));
       }

     }

     if (hingeTypeStack.size() == paneNum - 1)
     {
       gap += BYFOLD_NO_HINGE_GAP;
     }

     if (hingeTypeStack.size() == 1)
     {
       magnetSliceNum++;
     }

     while (hingeTypeStack.size() > 0)
     {
       typeChar = ((Character)hingeTypeStack.pop()).charValue();
       if (typeChar == 'S')
       {
         singleHingePaneNum++;
         gap += BYFOLD_SINGLE_HINGE_GAP;
       }
       if (typeChar == 'B')
       {
         bendHingePaneNum++;
         shuanglianPoleNum += 2;
         gap += BYFOLD_BEND_HINGE_GAP;
       }
       if (typeChar != 'D')
         continue;
       doubleHingePaneNum++;
       gap += BYFOLD_DOUBLE_HINGE_GAP;
     }

     poleNum = paneNum * 2 - duikouPoleNum - shuanglianPoleNum;

     info.setPaneNum(paneNum);
     info.setSingleHingePaneNum(singleHingePaneNum);
     info.setDoubleHingePaneNum(doubleHingePaneNum);
     info.setBendHingePaneNum(bendHingePaneNum);
     info.setPoleNum(poleNum);
     info.setDuikouPoleNum(duikouPoleNum);
     info.setShuanglianPoleNum(shuanglianPoleNum);
     info.setGap(gap);
     info.setMagnetSliceNum(magnetSliceNum);
     return info;
   }

   public static char getHingeInstallSides(String hingeInstallType, int pos)
   {
     char result = NONE_HINGE;
     String hingeInstallSide = hingeInstallType.toUpperCase().replaceAll("1", "");
     hingeInstallSide = hingeInstallSide.toUpperCase().replaceAll("S", "");
     hingeInstallSide = hingeInstallSide.toUpperCase().replaceAll("D", "");
     hingeInstallSide = hingeInstallSide.toUpperCase().replaceAll("B", "");
     result = hingeInstallSide.charAt(pos);
     return result;
   }

   public static float getHingeInstallGap(String hingeInstallType, int pos)
   {
     float gap = 0.0F;
     String hingeInstallSide = hingeInstallType.toUpperCase().replaceAll("1", "");
     hingeInstallSide = hingeInstallSide.toUpperCase().replaceAll("S", "");
     hingeInstallSide = hingeInstallSide.toUpperCase().replaceAll("D", "");
     hingeInstallSide = hingeInstallSide.toUpperCase().replaceAll("B", "");

     String hingeType = hingeInstallType.toUpperCase().replaceAll("1L1R", "1LN1R");
     hingeType = hingeType.toUpperCase().replaceAll("1", "");
     hingeType = hingeType.toUpperCase().replaceAll("R", "");
     hingeType = hingeType.toUpperCase().replaceAll("L", "");
     if (pos == 0)
     {
       if (hingeInstallSide.charAt(pos) == 'L')
       {
         gap = SINGLE_HINGE_GAP;
       }
       else
       {
         gap = NO_HINGE_GAP;
       }
     }
     else if (pos == hingeInstallSide.length() - 1)
     {
       if (hingeInstallSide.charAt(pos) == 'R')
       {
         gap = NO_HINGE_GAP;
       }
       else
       {
         gap = SINGLE_HINGE_GAP;
       }

     }
     else if ((hingeInstallSide.charAt(pos) == 'R') && (hingeInstallSide.charAt(pos - 1) == 'L'))
     {
       gap = DUIKOU_POLE_GAP;
     }
     else if (hingeType.charAt(pos) == 'B')
     {
       gap = BEND_HINGE_GAP;
     }
     else if (hingeType.charAt(pos) == 'D')
     {
       gap = DOUBLE_HINGE_GAP;
     }
     else if (hingeType.charAt(pos) == 'S')
     {
       gap = SINGLE_HINGE_GAP;
     }

     return gap;
   }

   public static String formatHingeInstallType(String hingeInstallType)
   {
     StringBuffer formatedInputBuff = new StringBuffer();
     hingeInstallType = hingeInstallType.trim().toUpperCase();
     String subStr = "";
     String sideStr = "";
     int i = 0;
     do
     {
       subStr = hingeInstallType.substring(i, i + 1);
       if ((CurtainChecker.isInteger(subStr)) && (new Integer(subStr).intValue() > 1))
       {
         if (i + 2 <= hingeInstallType.length())
         {
           sideStr = hingeInstallType.substring(i + 1, i + 2);
           if ((sideStr.equals("L")) || (sideStr.equals("R")))
           {
             for (int j = 0; j < new Integer(subStr).intValue(); j++)
             {
               formatedInputBuff.append("1").append(sideStr);
               if (j >= new Integer(subStr).intValue() - 1)
                 continue;
               formatedInputBuff.append("B");
             }

             i++;
           }
           else
           {
             formatedInputBuff.append(subStr);
           }
         }
       }
       else
       {
         formatedInputBuff.append(subStr);
       }
       i++;
     }
     while (i < hingeInstallType.length());
     return formatedInputBuff.toString();
   }

   public static void main(String[] args)
   {
     System.out.print(formatHingeInstallType("1lB1l1rb1r"));
     HingeRelatedInfo info1 = getByFoldHingeRelatedInfo("1LB1L1RB1R");
     System.out.println(info1.getMagnetSliceNum());
   }
 }

/* Location:           C:\Users\Administrator\Desktop\奥特百叶窗\Shutter DesignerV3.1\CurtainDesign.jar
 * Qualified Name:     CurtainDesign.utils.CalculateUtils
 * JD-Core Version:    0.6.0
 */